﻿@namespace AntDesign
@inherits AntInputComponentBase<TItemValue>
@typeparam TItemValue
@typeparam TItem

@using AntDesign.Internal
@using AntDesign.Select.Internal
@using Microsoft.AspNetCore.Components.Rendering

<CascadingValue Value="this" IsFixed="IsSelectFixed()">
    <CascadingValue Value=@("ant-select-dropdown") Name="PrefixCls" IsFixed>
        <OverlayTrigger @ref="@_dropDown" 
                            RefBack="RefBack"
                            Visible="@Open"
                            VisibleChanged="OpenChanged"
                            Disabled="Disabled"
                            Placement="@Placement"
                            Trigger="new[] { Trigger.Click }"
                            HiddenMode
                            ComplexAutoCloseAndVisible
                            BoundaryAdjustMode="@BoundaryAdjustMode"
                            OnMouseEnter="@(() => { OnMouseEnter?.Invoke(); })"
                            OnMouseLeave="@(() => { OnMouseLeave?.Invoke(); })"
                            OnVisibleChange="@OnOverlayVisibleChangeAsync"
                            PopupContainerSelector="@PopupContainerSelector"
                            Unbound="@(!UseChildContentAsTrigger || TriggerContent == null? (RenderFragment<ForwardRef>)RenderTriggerContent : null)"
                            ChildContent="@(UseChildContentAsTrigger && Unbound == null? TriggerContent : null)"
                            OverlayClassName="@OverlayClassName"
                            OverlayEnterCls="ant-slide-up-enter ant-slide-up-enter-active ant-slide-up"
                            OverlayLeaveCls="ant-slide-up-leave ant-slide-up-leave-active ant-slide-up">
                <Overlay>
                    <div style="@_dropdownStyle">
                        @if (DropdownRender != null)
                        {
                            @DropdownRender(RenderOptionDropdown())
                        }
                        else
                        {
                            @RenderOptionDropdown()
                        }
                    </div>                    
                </Overlay> 
            </OverlayTrigger>
    </CascadingValue>
</CascadingValue>

@code {
    protected abstract RenderFragment RenderOptionDropdown();

    protected virtual RenderFragment RenderTriggerContent(ForwardRef forwardRef)
    {
        return builder => UnboundContent(forwardRef, builder);
    }
    private void UnboundContent(ForwardRef context, RenderTreeBuilder __builder)
    {
        @if (Unbound!=null)
        {
            @Unbound(context)
        }
        else
        {
            <CascadingValue Value="this" Name="ParentSelect" IsFixed>
                <CascadingValue Value="@LabelTemplate" Name="ParentLabelTemplate">
                    <CascadingValue Value="@MaxTagPlaceholder" Name="ParentMaxTagPlaceholerTemplate">
                        <CascadingValue Value="@ShowSearchIcon" Name="ShowSearchIcon">
                            <CascadingValue Value="@ShowArrowIcon" Name="ShowArrowIcon">
                                <SelectContent Prefix="@ClassPrefix"
                                               RefBack="@context"
                                               @ref="_selectContent"
                                               TItemValue="TItemValue"
                                               TItem="TItem"
                                               SearchValue="@_searchValue"
                                               SearchDebounceMilliseconds="@SearchDebounceMilliseconds"
                                               IsOverlayShow="@_dropDown.IsOverlayShow()"
                                               MaxTagCount="@_maxTagCountAsInt"
                                               OnInput="@OnInputAsync"
                                               OnKeyUp="@OnKeyUpAsync"
                                               OnKeyDown="@OnKeyDownAsync"
                                               OnFocus="@OnInputFocusAsync"
                                               OnBlur="@OnInputBlurAsync"
                                               OnClearClick="@OnInputClearClickAsync"
                                               OnRemoveSelected="@OnRemoveSelectedAsync"
                                               Placeholder="@Placeholder" />
                            </CascadingValue>
                        </CascadingValue>
                    </CascadingValue>
                </CascadingValue>
            </CascadingValue>
        }
    }
}
